package com.xcf.controller.business.finance;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xcf.controller.base.BaseController;
import com.xcf.service.business.finance.BusinessAccountApplyService;
import com.xcf.service.business.finance.CostCheckListService;
import com.xcf.service.business.finance.FreightIncomeService;
import com.xcf.util.PageData;
import com.xcf.util.ResponseResult;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@RequestMapping(value = "/businessAccountApply")
public class BusinessAccountApplyController extends BaseController{
	@Resource
	private BusinessAccountApplyService businessAccountApplyService;
	@Resource
	private CostCheckListService costCheckListService;
	@Resource
	private FreightIncomeService freightIncomeService;
	/**
	 * 页面跳转
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/list")
	public ModelAndView businessAccountApplyPage()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName("/business/finance/accountingChanges/business_account_apply_list");
		return view;
		
	}
	/**
	 * 页面跳转
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/record")
	public ModelAndView businessAccountApplyRecord()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName("/business/finance/accountingChanges/business_account_apply_record");
		return view;
		
	}
	/**
	 * 页面跳转
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/examine")
	public ModelAndView businessAccountApplyExamine()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName("/business/finance/accountingChanges/business_account_apply_examine");
		return view;
		
	}
	/**
	 * 预付信息查询
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findBusinessAccountApplyInfo")
	@ResponseBody
	public ResponseResult findBusinessAccountApplyInfo(int page) throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = new ArrayList<Map>();
		if(pd.getString("state").equals("1")){
			list = businessAccountApplyService.findBusinessAccountApply(pd);
		}else if(pd.getString("state").equals("0")){
			list = businessAccountApplyService.findIncomeInfo(pd);
			
		}else{
			list = businessAccountApplyService.findReceivablesCostInfo(pd);
		}
		
		return ResponseResult.success(new PageInfo(list));
	}
	/**
	 * 查找收（付）款单号
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findAppCodeInfo")
	@ResponseBody
	public ResponseResult findAppCodeInfo(int page)throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = businessAccountApplyService.findAppCodeInfo();
		return ResponseResult.success(new PageInfo(list));
		
	}
	
	/**
	 * 保存信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveBusinessAccountApplyInfo")
	@ResponseBody
	public String saveBusinessAccountApplyInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		JSONArray items = JSONArray.fromObject(pd.getString("array"));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		PageData b = new PageData();
		b.put("app_code", null);
		Object appCode = businessAccountApplyService.getAppCode(b);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			PageData pdq = new PageData();
			pdq.put("bill_code", item.getString("bill_code"));
			if(item.getString("amount")==null || item.getString("amount")==""||item.getString("amount").equals("")||item.getString("amount").equals("null")){
				
			}else{
				float a = Float.parseFloat(item.getString("amount"));
				pdq.put("amount", a);
			}
			
			pdq.put("app_code", appCode);
			if(pd.getString("advance_pay_amount")==null || pd.getString("advance_pay_amount")==""||pd.getString("advance_pay_amount").equals("")||pd.getString("advance_pay_amount").equals("null")){
				
			}else{
				float a = Float.parseFloat(pd.getString("advance_pay_amount"));
				pdq.put("advance_pay_amount", a);
			}
			pdq.put("app_change_reason", pd.getString("app_change_reason"));
			pdq.put("payment_type", pd.getString("payment_type"));
			pdq.put("pay_receivables_code", pd.getString("pay_receivables_code"));
			pdq.put("currency", pd.getString("currency"));
			pdq.put("app_time", df.format(new Date()));
			businessAccountApplyService.saveBusinessAccountApply(pdq);
		}
		return null;
		
	}
	
	/**
	 * 业务台账变更申请记录
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findRecordInfo")
	@ResponseBody
	public ResponseResult findRecordInfo(int page) throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = businessAccountApplyService.findRecordInfo();
		return ResponseResult.success(new PageInfo(list));
	}
	/**
	 *业务台账变更申请记录明细
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findRecordDetail")
	@ResponseBody
	public ResponseResult findRecordDetail(int page) throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = businessAccountApplyService.findRecordDetail(pd);
		return ResponseResult.success(new PageInfo(list));
	}
	/**
	 * 更新状态
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updateState")
	@ResponseBody
	public String updateState()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		if(pd.getString("app_state").equals("1")){
			if(pd.getString("payment_type").equals("1")||pd.getString("payment_type").equals("0")){
				PageData b = new PageData();
				b.put("paymentCode", null);
				Object pp = costCheckListService.getPaymentCode(b);
				String pp1 = pp.toString();
				pd.put("pp1", pp1);
				pd.put("payment_app_code", pp1);
				if(pd.getString("payment_type").equals("1")){
					pd.put("is_income_loan", "1");
				}else{
					pd.put("is_income_loan", "0");
				}
				//查出借款明细
				PageData business = businessAccountApplyService.findCostInfoLoanByBillCode(pd.getString("bill_code"));
				//保存进借款记录表
				costCheckListService.saveCostLoanRecordDetail(business);
				//根据业务编号删除借款明细
				businessAccountApplyService.deleteCostInfoLoan(pd.getString("bill_code"));
				//保存公共信息表
				costCheckListService.saveCostInfo(pd);
				
				List<Map> list = businessAccountApplyService.findRecordDetail(pd);
				//在将页面的预收信息保存到预收表
				businessAccountApplyService.saveCostLoanInfo(list);
			}else if(pd.getString("payment_type").equals("2")){
				PageData b = new PageData();
				b.put("paymentCode", null);
				Object pp = freightIncomeService.getPaymentCode(b);
				String pp1 = pp.toString();
				pd.put("pp1", pp1);
				pd.put("receipt_app_code", pp1);
				PageData business = businessAccountApplyService.findReceivablesCostDetail(pd);
				businessAccountApplyService.saveReceivablesCostDetailRecord(business);
				businessAccountApplyService.deleteReceivablesCostDetailRecord(pd);
				businessAccountApplyService.saveReceivablesCost(pd);
				List<Map> list = businessAccountApplyService.findRecordDetailByAppCode(pd);
				for(Map m : list){
					PageData pdq = new PageData();
					pdq.put("receipt_app_code", m.get("receipt_app_code").toString());
					pdq.put("bill_code", m.get("bill_code").toString());
					if(m.get("currency").equals("RMB")){
						pdq.put("rmb_amount", m.get("amount"));
					}else if(m.get("currency").equals("USD")){
						pdq.put("usd_amount", m.get("amount"));
					}
					businessAccountApplyService.saveReceivablesCostDetail(pdq);
				}
//				businessAccountApplyService.saveReceivablesCostDetail(list);
			}
			
		}
			
		businessAccountApplyService.updateState(pd);
		return null;
	}
	/**
	 *业务台账变更申请记录明细
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findRecordData")
	@ResponseBody
	public ResponseResult findRecordData(int page) throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = businessAccountApplyService.findRecordData();
		return ResponseResult.success(new PageInfo(list));
	} 
}
